<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/11/25
 * @Description: 环信用户好友关系表
 */

namespace App\Models;

use App\Http\Controllers\WebApi\Business\ConfigBusiness;
use App\Jobs\FriendTask;
use App\Services\Configs\DefaultService;
use App\Services\Netease\GroupServices as NetGroupServices;
use App\Services\RongCloud\GroupService;
use Illuminate\Support\Facades\DB;
use AlibabaCloud\CDRS\V20201101\PaginateProject;
use App\Services\Huanxin\GroupServices;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Services\Huanxin\UserServices;
use App\Services\RongCloud\UserService as RongUserService;
use App\Services\Netease\UserServices as NeteaseUserService;
use Illuminate\Support\Facades\Redis;

class ImUserFriend extends BaseModel
{
    use SoftDeletes;
    protected $table = 'im_user_friend';
    protected $hidden = ['deleted_at'];

    /**
     * 关联用户表
     */
    public function user()
    {
        return $this->belongsTo(ImUsers::class, 'other_uid', 'id');
    }

    /**
     * 关联用户表
     */
    public function other()
    {
        return $this->belongsTo(ImUsers::class, 'user_id', 'id');
    }

    /**
     * 关联groupuser
     */
    public function group_user()
    {
        return $this->hasMany(ImGroupUser::class, 'user_id', 'other_uid');
    }

    /**
     * @desc 添加好友关系
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function addFriend($input, $userId)
    {
        $otherFriend = $this->where(['user_id' => $input['other_uid'], 'other_uid' => $userId])->first();
        if ($otherFriend && $otherFriend['status'] == 3)
        {
            return ['code' => 1, 'msg' => '你已被对方拉黑, 不能申请为好友'];
        }

        $userFriend = $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->first();
        if ($userFriend && $userFriend['status'] == 3)
        {
            return ['code' => 1, 'msg' => '你已拉黑对方, 不能添加对方为好友'];
        }

        if ($otherFriend && $otherFriend['status'] == 0)
        {
            return ['code' => 0, 'msg' => '发送成功'];
        }

        if (!$userFriend)
        {
            $ImUserFriend = new ImUserFriend();
            $ImUserFriend->user_id = $userId;
            $ImUserFriend->other_uid = $input['other_uid'];
            $ImUserFriend->note_name = $input['note_name'] ?? '';
            $ImUserFriend->apply_note = $input['apply_note'] ?? '';
            $ImUserFriend->source = $input['source'] ?? 1; // 好友来源：1扫一扫，2搜索添加，
            $ImUserFriend->circle_status = $input['no_look_other'] ?? 1; // 不看他
            $ImUserFriend->save();
        }

        if (!$otherFriend)
        {
            $otherUserFriend = new ImUserFriend();
            $otherUserFriend->user_id = $input['other_uid'];
            $otherUserFriend->other_uid = $userId;
            $otherUserFriend->no_allow_status = $input['no_allow_look'] ?? 1; // 不让他看
            $otherUserFriend->source = $input['source'] ?? 1; // 好友来源：1扫一扫，2搜索添加，
            $otherUserFriend->save();
        }

        return ['code' => 0, 'id' => $this->id, 'msg' => '发送成功'];
    }

    /**
     * @desc 审核好友关系
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function auditFriend($input, $userId)
    {
        FriendTask::dispatch(['user_id'=>$userId, 'other_uid'=>$input['other_uid']]);

        $noAllowLook = $input['no_allow_look'] ?? 1; // 不让他看
        $noLookOther = $input['no_look_other'] ?? 1; // 不看他
        $noteName = $input['note_name'] ?? ''; // 备注

        $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->update(['status' => 1, 'circle_status' => $noLookOther, 'note_name' => $noteName]);
        $this->where(['user_id' => $input['other_uid'], 'other_uid' => $userId])->update(['status' => 1, 'no_allow_status' => $noAllowLook]);

        return ['code' => 0, 'msg' => '添加成功'];

    }

    /**
     * @desc 删除好友
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function delFriend($input, $userId = 0)
    {
        $option = ConfigBusiness::thirdPartyFirst('im');
        $optionValue = $option->value;
        switch($optionValue)
        {
            case ($optionValue == DefaultService::HUANXIN_IM):  // 环信
                $userServices= new UserServices();
                $userServiceInfo = $userServices->deleteFriend($userId,$input['other_uid']);
                break;
            case ($optionValue == DefaultService::RONGYUN_IM):  // 融云
                $userServiceInfo['code'] = 200;
                break;
            case ($optionValue == DefaultService::NETEASE_IM):  // 网易
                $userServices= new NeteaseUserService();
                $userServiceInfo = $userServices->deleteFriend($userId,$input['other_uid'] );
                break;
            default:
                $userServiceInfo['code'] = 400;
                break;
        }

        if ($userServiceInfo['code'] != 200)
        {
            return ['code' => 1, 'msg' => '删除失败'];
        }

        $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->delete();

        // 朋友圈状态改成不可看 但是单向好友不动
        $this->where(['user_id' => $input['other_uid'], 'other_uid' => $userId])->update(['no_allow_status' => 2]);

        return ['code' => 0, 'msg' => '删除成功'];
    }

    /**
     * @desc 好友列表
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function friendList($userId = 0, $limit = 10, $offset = 0)
    {
        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->with(['user' => function($q){
            $q->select('id','nickname','headimg');
        }])->where(['user_id'=>$userId, 'status'=>1])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return $list;
    }

    /**
     * @desc 黑名单管理
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function blackFriendList($userId = 0, $limit = 10, $offset = 0)
    {
        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->with(['user' => function($q){
            $q->select('id','nickname','headimg');
        }])->where(['user_id'=>$userId, 'status'=>3])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return $list;
    }

    /**
     * @desc 好友关系拉黑
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function addUserBlack($input, $userId = 0)
    {
        $option = ConfigBusiness::thirdPartyFirst('im');
        $optionValue = $option->value;
        switch($optionValue)
        {
            case ($optionValue == DefaultService::HUANXIN_IM):  // 环信
                $userServices= new UserServices();
                $userServiceInfo = $userServices->addUserForBlacklist($input['other_uid'], $userId);
                break;
            case ($optionValue == DefaultService::RONGYUN_IM):  // 融云
                $userServices= new RongUserService();
                $userServiceInfo = $userServices->addBlack($userId, $input['other_uid']);
                break;
            case ($optionValue == DefaultService::NETEASE_IM):  // 网易
                $userServices= new NeteaseUserService();
                $userServiceInfo = $userServices->specializeFriend($userId,$input['other_uid']);
                break;
            default:
                $userServiceInfo['code'] = 400;
                break;
        }

        if ($userServiceInfo['code'] != 200)
        {
            return ['code' => 1, 'msg' => '拉黑失败'];
        }

        $userFriend = $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->first();
        if (!$userFriend)
        {
            return ['code' => 1, 'msg' => '关系不存在'];
        }

        $userFriend->status = 3;
        $userFriend->save();

        // 朋友圈状态改成不可看 但是单向好友不动 拉黑不让对方看朋友圈
        $this->where(['user_id' => $input['other_uid'], 'other_uid' => $userId])->update(['no_allow_status' => 2]);

        return ['code' => 0, 'msg' => '拉黑成功'];
    }

    /**
     * @desc 好友关系移除黑名单
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function delUserBlack($input, $userId = 0)
    {
        $option = ConfigBusiness::thirdPartyFirst('im');
        $optionValue = $option->value;
        switch($optionValue)
        {
            case ($optionValue == DefaultService::HUANXIN_IM):  // 环信
                $userServices= new UserServices();
                $userServiceInfo = $userServices->deleteUserFromBlacklist($userId, $input['other_uid']);
                break;
            case ($optionValue == DefaultService::RONGYUN_IM):  // 融云
                $userServices= new RongUserService();
                $userServiceInfo = $userServices->removeBlack($userId, $input['other_uid']);
                break;
            case ($optionValue == DefaultService::NETEASE_IM):  // 网易
                $userServices= new NeteaseUserService();
                $userServiceInfo = $userServices->specializeFriend($userId,$input['other_uid'],1, 0 );
                break;
            default:
                $userServiceInfo['code'] = 400;
                break;
        }

        if ($userServiceInfo['code'] != 200)
        {
            return ['code' => 1, 'msg' => '拉黑失败'];
        }

        $userFriend = $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->first();
        if (!$userFriend)
        {
            return ['code' => 1, 'msg' => '关系不存在'];
        }

        if ($userFriend['status'] != 3)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        $userFriend->status = 1;
        $userFriend->save();

        // 朋友圈状态改成不可看 但是单向好友不动，允许让对方看朋友圈
        $this->where(['user_id' => $input['other_uid'], 'other_uid' => $userId])->update(['no_allow_status' => 1]);

        return ['code' => 0, 'msg' => '操作成功'];
    }

    /**
     * @desc 新好友列表
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function newFriendList($userId = 0, $limit = 20, $offset = 0)
    {
        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb','apply_note','created_at')->with(['user' => function($q){
            $q->select('id','nickname','headimg');
        }])->where(['user_id'=>$userId])
            ->whereIn('status', [0,1])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get()->map(function($val) {
                $val['is_expired'] = 0;
                if ($val['created_at'] < date('Y-m-d H:i:s',strtotime("-10 day")))
                {
                    $val['is_expired'] = 1;
                }

                return $val;
            });

        return $list;
    }

    /**
     * @desc 手机联系人
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function phoneFriendList($input, $userId = 0)
    {
        $phones = $input['phone'];
        $phones = json_decode($phones,true);

        $names = $input['name'];
        $names = json_decode($names,true);

        $phoneUser = [];
        foreach ($phones as $k =>&$v)
        {
            $phoneUser[$k]['phone'] = $v;
            $phoneUser[$k]['name'] = $names[$k];
        }

        // user表查询手机号
        $user =new User();
        $phonesList = $user->getUserByPhone($phones);
        $phonesList = array_column($phonesList, null, 'phone');

        // 查询我所有的好友
        $userFriendList = $this->getUserFriednByUid($userId);
        $userFriendList = array_column($userFriendList, null, 'other_uid');
        foreach ($phoneUser as $key =>&$val)
        {
            $val['user_id'] = $phonesList[$val['phone']]['id'] ?? 0;
            $val['headimg'] = $phonesList[$val['phone']]['headimg'] ?? "";
            $val['name'] = $val['name'];
            $val['im_number'] = $phonesList[$val['phone']]['im_number'] ?? '';
            $val['status'] = !empty($userFriendList[$val['user_id']]) ? 1 : 0; // 1已添加 0未添加

        }

        return $phoneUser;
    }

    /**
     * @desc 通过uid查询我所有的好友
     * @param int $uid uid
     * @return array
     * @author zhouzhou
     * @time 2021/12/1 11:21
     */
    public function getUserFriednByUid($uid)
    {
        if (empty($uid))
        {
            return [];
        }

        $query = $this->where(['user_id'=>$uid,'status'=>1]);
        $list = $query->select('id','user_id','status','other_uid')
            ->get()->toArray();
        if ($list)
        {
            return $list;
        }

        return [];
    }

    /**
     * @desc 设置好友权限
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function setFriendAllow($input, $userId)
    {
        $userFriend = $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->first();
        if (!$userFriend)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        $noAllowLook = $input['no_allow_look'] ?? 1; // 不让他看
        $noLookOther = $input['no_look_other'] ?? 1; // 不看他

        $userFriend->circle_status = $noLookOther; // 不看他
        $userFriend->save();

        // 不让他看
        $this->where(['user_id' => $input['other_uid'], 'other_uid' => $userId])->update(['no_allow_status' => $noAllowLook]);

        return ['code' => 0, 'id' => $this->id, 'msg' => '修改成功'];
    }

    /**
     * @desc 设置好友备注
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function setFriendNote($input, $userId)
    {
        $userFriend = $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->first();
        if (!$userFriend)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        $userFriend->note_name = $input['note_name'] ?? ''; // 备注
        $userFriend->save();

        return ['code' => 0, 'id' => $this->id, 'msg' => '修改成功'];
    }

    /**
     * @desc 设置好友聊天背景
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function setFriendCover($input, $userId)
    {
        $userFriend = $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->first();
        if (!$userFriend)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        $userFriend->chat_cover = $input['cover'] ?? ''; // 聊天背景
        $userFriend->save();

        return ['code' => 0, 'id' => $this->id, 'msg' => '修改成功'];
    }

    /**
     * @desc 查看好友资料
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function goodFriend($input, $userId)
    {
        $userFriend = $this->select('id', 'user_id', 'other_uid', 'circle_status', 'no_allow_status', 'note_name', 'source', 'top', 'no_disturb', 'status')->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->first();

        $otherUserFriend = $this->where(['user_id' => $input['other_uid'], 'other_uid' =>$userId])->first();

        $ohterUser = new User();
        $ohterUser = $ohterUser->select('id', 'user_type', 'nickname', 'im_number', 'phone', 'headimg', 'sex', 'personalized_signature','province_id','city_id', 'area_id', 'address')->with(['province','city'])->find($input['other_uid']);

        // 查看自己
        if ($userId != $input['other_uid'])
        {
            $userFriend->no_allow_look = $otherUserFriend['no_allow_status']; // 不让他看状态 1 可看 2 不让他看 这两个只是自己主动设置的按钮状态
        }

        $imGroupUser = new ImGroupUser();
        // 查询对方的隐私设置，
        $ohterUser->firend_circle = $ohterUser->secrecy()->first()->firend_circle;
        $userFriend->common_group_count = $imGroupUser->groupComotCount($input['other_uid'], $userId);

        // 对方不给你看，直接返回,过滤掉自己
        if ($userFriend['no_allow_status'] == 2 && $userId != $input['other_uid'] )
        {
            return ['user'=>$ohterUser, 'friend'=>$userFriend];
        }

        // 他的动态最近四张图片和视频
        $imFriendsCircle = new ImFriendsCircle();
        $imFriendsCircle = $imFriendsCircle->select('id', 'user_id', 'content', 'type', 'created_at', 'reminds', 'choose_user', 'visiable')->with(['imgs'])
            ->where(['is_del'=>2, 'status'=>1, 'user_id'=>$input['other_uid']])->whereIn('type', [2,3])
            ->orderBy('id', 'desc')
            ->limit(4)
            ->get();

        foreach ($imFriendsCircle as $key =>&$val)
        {
            $val['show'] = 0; // 不显示
            $val['show_flag'] = 0;

            // 根据不同状态visiable字段展示，这里没有判断超过半年，3天，下面判断
            switch (intval($val['visiable']))
            {
                case 1:
                    $val['show'] = 1; // 公开
                    break;
                case 2:
                    if ($val['user_id'] == $userId)
                    {
                        $val['show'] = 1; // 2 私密
                        $val['show_flag'] = 1; // 仅某人可见
                    }
                    break;
                case 3:
                    $chooseUser = explode(",",$val['choose_user']);
                    if (in_array($userId, $chooseUser))
                    {
                        $val['show'] = 1; // 仅某人可见
                        $val['show_flag'] = 1; // 2 私密
                    }
                    break;
                case 4:
                    $chooseUser = explode(",",$val['choose_user']);
                    if (!in_array($userId, $chooseUser))
                    {
                        $val['show'] = 1; // 不给某人可见，不在这里可以看见
                        $val['show_flag'] = 1; // 2 私密
                    }
                    break;
                default:
                    $val['show'] = 0;
                    break;
            }

            // 这里判断时间超过30天，180天
            if ($userId == $ohterUser->id)
            {
                $val['show'] = 1; // 显示
            }
            else if ($userFriend->no_allow_status == 2)
            {
                $val['show'] = 0; // 不显示
            }
            else if($ohterUser->firend_circle == 2)
            {
                $val['show'] = $this->showFlagTime($val['show_flag'], $val['created_at'], 180, $val['visiable']);
            }
            else if($ohterUser->firend_circle == 3)
            {
                $val['show'] = $this->showFlagTime($val['show_flag'], $val['created_at'], 30, $val['visiable']);
            }
            else if($ohterUser->firend_circle == 4)
            {
                $val['show'] = $this->showFlagTime($val['show_flag'], $val['created_at'], 3, $val['visiable']);
            }

            if ($val['show'] == 0)
            {
                unset($imFriendsCircle[$key]);
            }
        }

        $imFriendsCircle = array_values($imFriendsCircle->toArray());

        return ['user'=>$ohterUser, 'circle'=>$imFriendsCircle, 'friend'=>$userFriend];
    }

    /**
     * @desc 设置单聊免打扰 status 1
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function setFriendDisturb($input, $userId)
    {
        $userFriend = $this->where(['user_id' => $userId, 'other_uid' => $input['other_uid']])->first();
        if (!$userFriend)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        $userFriend->no_disturb = $input['status'] ?? 0; // 1是免打扰， 0默认打扰
        $userFriend->save();

        return ['code' => 0, 'id' => $this->id, 'msg' => '设置成功'];
    }

    /**
     * @desc 权限，不让对方看
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function noAllowLookList($userId = 0, $limit = 10, $offset = 0)
    {
        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->with(['other' => function($q){
            $q->select('id','nickname','headimg');
        }])->where(['other_uid'=>$userId, 'no_allow_status'=>2])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        $count = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->where(['other_uid'=>$userId, 'no_allow_status'=>2])
            ->count();

        return ['list'=>$list, 'count'=>$count];
    }

    /**
     * @desc 权限，不看对方
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function noLookOtherList($userId = 0, $limit = 10, $offset = 0)
    {
        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->with(['user' => function($q){
            $q->select('id','nickname','headimg');
        }])->where(['user_id'=>$userId, 'circle_status'=>2])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        $count = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->where(['user_id'=>$userId, 'circle_status'=>2])
                ->count();

        return ['list'=>$list, 'count'=>$count];
    }

    /**
     * @desc 不让对方看我的朋友圈移除，支持多个
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/6 14:58
     */
    public function delAllowLook($input, $userId = 0)
    {
        $ids = $input['id'];
        $ids = explode(",",$ids);

        // 改成允许对方看朋友圈
        $this->where(['other_uid' => $userId])->whereIn('id', $ids)->update(['no_allow_status' => 1]);

        return ['code' => 0, 'msg' => '操作成功'];
    }

    /**
     * @desc 添加多个不让对方看我的朋友圈，支持多个 传对方的other_uid，多个逗号隔开
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/6 14:58
     */
    public function addAllowLook($input, $userId = 0)
    {
        $otherUids = $input['other_uid'];
        $otherUids = explode(",",$otherUids);

        // 不允许对方看朋友圈
        $this->where(['other_uid' => $userId])->whereIn('user_id', $otherUids)->update(['no_allow_status' => 2]);

        return ['code' => 0, 'msg' => '操作成功'];
    }

    /**
     * @desc 不看对方的朋友圈移除，支持多个，
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/6 14:58
     */
    public function delLookOther($input, $userId = 0)
    {
        $ids = $input['id'];
        $ids = explode(",",$ids);

        // 跟新看对方的朋友圈
        $this->where(['user_id' => $userId])->whereIn('id', $ids)->update(['circle_status' => 1]);

        return ['code' => 0, 'msg' => '操作成功'];
    }

    /**
     * @desc 不看对方的朋友圈添加，支持多个 逗号隔开
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/6 14:58
     */
    public function addLookOther($input, $userId = 0)
    {
        $otherUids = $input['other_uid'];
        $otherUids = explode(",",$otherUids);

        // 不允许对方看朋友圈
        $this->where(['user_id' => $userId])->whereIn('other_uid', $otherUids)->update(['circle_status' => 2]);

        return ['code' => 0, 'msg' => '操作成功'];
    }

    /**
     * @desc 朋友圈可看公共方法
     * @param int $showFlag 私密显示
     * @param int $createTime 时间
     * @param int $visiable 这条朋友圈可看类型 1公开 2私密 3仅某人可见4不给某人看
     * @param int $day 天数
     * @author zhouzhou
     * @time 2021/12/7 17:43
     */
    public function showFlagTime($showFlag = 0, $createTime, $day, $visiable)
    {
        // 如果标记可看，但是不在时间内
        if ($showFlag == 1)
        {
            // 最近三天
            if ($createTime < date('Y-m-d H:i:s',strtotime("-{$day} day")))
            {
                $show = 0;
            }
            else
            {
                $show = 1;
            }
        }
        else
        {
            // 如果标记可不看之内，展示0，不判断时间
            if (in_array($visiable, [2,3,4]))
            {
                $show = 0;
            }
            else
            {
                // 如果是公开， 在判断时间
                if ($createTime >= date('Y-m-d H:i:s',strtotime("-{$day} day")))
                {
                    $show = 1;
                }
                else
                {
                    $show = 0;
                }
            }
        }

        return $show;
    }

    /**
     * @desc 群添加的好友列表
     * @param string $imGid 群组的id
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function groupFriendList($imGid, $userId = 0, $limit = 10, $offset = 0)
    {
        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->with(['user' => function($q){
            $q->select('id','nickname','headimg');
        }])->with(['group_user' => function($q) use ($imGid) {
            $q->select('im_gid','user_id');
            $q->where('im_gid', $imGid);
        }])->where(['user_id'=>$userId, 'status'=>1])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return $list;
    }

    /**
     * @desc 不允许对方看的好友列表
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function noAllowFriendList($userId = 0, $limit = 10, $offset = 0)
    {
        // 查询所有不让对方看的一维数组
        $userIdList = $this->select('user_id')->where(['other_uid'=>$userId, 'no_allow_status'=>2])
            ->get()->pluck('user_id')->all();

        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->with(['user' => function($q){
            $q->select('id','nickname','headimg');
        }])->where(['user_id'=>$userId, 'status'=>1])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get()->map(function($val) use($userIdList){
                $val['is_choose'] = 0;
                if (in_array($val['other_uid'], $userIdList))
                {
                    $val['is_choose'] = 1;
                }

                return $val;
            });

        return $list;
    }

    /**
     * @desc 不看对方好友列表
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function noLookFriendList($userId = 0, $limit = 10, $offset = 0)
    {
        // 查询所有不看对方一维数组
        $otherUidList = $this->select('other_uid')->where(['user_id'=>$userId, 'circle_status'=>2])
            ->get()->pluck('other_uid')->all();

        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->with(['user' => function($q){
            $q->select('id','nickname','headimg');
        }])->where(['user_id'=>$userId, 'status'=>1])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get()->map(function($val) use($otherUidList){
                $val['is_choose'] = 0;
                if (in_array($val['other_uid'], $otherUidList))
                {
                    $val['is_choose'] = 1;
                }

                return $val;
            });

        return $list;
    }


    /**
     * @desc 好友列表
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function allFriendList()
    {
        $list = $this->select('id', 'user_id', 'other_uid', 'status', 'no_disturb')->where(['status'=>1])
            ->orderBy('id', 'desc')
            ->get();

        return $list;
    }

}
